<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\ProductArea;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\services\Chain;

class Up
{
    protected $order;
    protected $user;

    protected $set;

    protected $first_order_item;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();

        $this->first_order_item = $first_order_item;

        $product_id = $first_order_item->product_id;

        $this->set = SetUserUp::getLastOne();

        $is_bao = ProductArea::checkBao($product_id);
        $is_presale = ProductArea::checkPresale($product_id);

        // 必须是报单区
        if ($is_bao) {
            $this->upMyself();
            $this->upChain();
        }

        // 预售这个升级不用了
        // if ($is_presale) {
        //     if ($this->order->active_ok == 1) {
        //         $this->upMyself(true);
        //         $this->upChain();
        //     }
        // }

        // 处理关系链上的待返(升完级后就处理了)
        // $this->awaitCommission();

    }

    protected function awaitCommission()
    {
        $chain = Chain::getReferrerChain($this->user->id, [
            UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
            UserLevel::$keys['vip3'],
            UserLevel::$keys['vip4']
        ], true);

        foreach ($chain as $user) {
            $has = Award::hasHelp($user->id);
            if ($has) {
                $this->saveCommissionAwait($user->id);
            }
        }
    }

    // 升级自己
    protected function upMyself($active = false)
    {
        if ($active || $this->first_order_item->price >= $this->set->vip1) {
            if ($this->user->level < UserLevel::$keys['vip1']) {
                $this->user->level = UserLevel::$keys['vip1'];
                $this->user->save();
            }
        }

    }

    // 升级关系链
    protected function upChain()
    {
        $chain = Chain::getReferrerChain($this->user->id, [
            UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
            UserLevel::$keys['vip3'],
            UserLevel::$keys['vip4']
        ], true);

        foreach ($chain as $user) {
            if ($user->level == UserLevel::$keys['vip1']) {
                $vip1 = Chain::getChildrenLevel($user->id, [
                    UserLevel::$keys['vip1'],
                    UserLevel::$keys['vip2'],
                    UserLevel::$keys['vip3'],
                    UserLevel::$keys['vip4']
                ]);
                if ($vip1 && count($vip1) >= $this->set->vip2) {
                    $user->level = UserLevel::$keys['vip2'];
                    $this->saveUser($user);

                    // 处理20%待返
                    // $this->saveCommissionAwait($user->id);
                }
            }
            // if ($user->level == UserLevel::$keys['vip2']) {
            //     $vip2 = Chain::getChildrenLevel($user->id, [
            //         UserLevel::$keys['vip1'],
            //         UserLevel::$keys['vip2'],
            //         UserLevel::$keys['vip3'],
            //         UserLevel::$keys['vip4']
            //     ]);
            //     $has_vip2 = Chain::getChildrenLevel($user->id, [
            //         // UserLevel::$keys['vip1'],
            //         UserLevel::$keys['vip2'],
            //         UserLevel::$keys['vip3'],
            //         UserLevel::$keys['vip4']
            //     ]);
            //
            //     if ($vip2 && $has_vip2 && count($has_vip2) >= $this->set->vip3) {
            //
            //         // 判断前2个是不是创1
            //         if (count($vip2) >= 2
            //             && $vip2[0]['level'] >= UserLevel::$keys['vip2']
            //             && $vip2[1]['level'] >= UserLevel::$keys['vip2']) {
            //             $user->level = UserLevel::$keys['vip3'];
            //             $this->saveUser($user);
            //         }
            //     }
            // }
            // if ($user->level == UserLevel::$keys['vip3']) {
            //     $vip3 = Chain::getChildrenLevel($user->id, [
            //         UserLevel::$keys['vip1'],
            //         UserLevel::$keys['vip2'],
            //         UserLevel::$keys['vip3'],
            //         UserLevel::$keys['vip4']
            //     ]);
            //     $has_vip3 = Chain::getChildrenLevel($user->id, [
            //         // UserLevel::$keys['vip1'],
            //         // UserLevel::$keys['vip2'],
            //         UserLevel::$keys['vip3'],
            //         UserLevel::$keys['vip4']
            //     ]);
            //     if ($vip3 && $has_vip3 && count($has_vip3) >= $this->set->vip4) {
            //
            //         // 判断前2个是不是创2
            //         if (count($vip3) >= 2
            //             && $vip3[0]['level'] >= UserLevel::$keys['vip3']
            //             && $vip3[1]['level'] >= UserLevel::$keys['vip3']) {
            //             $user->level = UserLevel::$keys['vip4'];
            //             $this->saveUser($user);
            //         }
            //     }
            // }
        }
    }

    // 每个人升级, 都要处理待返
    private function saveUser(User $user)
    {
        $user->save();

        // 条件成立
        $has = Award::hasHelp($user->id);
        if ($has) {
            $this->saveCommissionAwait($user->id);
        }
    }

    protected function saveCommissionAwait($user_id)
    {
        // 取出待返
        $commissions = UserCommission::find()
            ->where(['user_id' => $user_id])
            ->andWhere(['status' => 0])
            // ->andWhere(['award_type' => 201])
            ->all();

        $account = UserAccount::findOne(['user_id' => $user_id]);

        foreach ($commissions as $commission) {
            $commission->status = 1;
            $commission->save();

            $account->balance += $commission->balance;
            $account->await_balance -= $commission->balance;
            $account->save();
        }
    }

}